package com.orhon.smartcampus.modules.material.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.material.dao.UnitDao;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Unit;
import com.orhon.smartcampus.modules.material.gqlModel.GQLBuilding;
import com.orhon.smartcampus.modules.material.gqlModel.GQLUnit;
import com.orhon.smartcampus.modules.material.mm.BuildingMapper;
import com.orhon.smartcampus.modules.material.mm.UnitMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
@Component
public class UnitListFetcher  implements DataFetcher {
	@Autowired
	private UnitDao unitDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		List<GQLUnit> GQLUnit = new ArrayList<>();
		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);

		String hql = "from Unit where deleted_at = null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("building_id")!=null) {
				hql += "and buildings_id" + Util.decide(map.get("building_id"));
			}
			if(map.get("code")!=null) {
				hql += "and unit_code  " +Util.decide( map.get("code"));
			}
			if(map.get("tel")!=null) {
				hql += "and  unit_tel " + Util.decide(map.get("tel"));
			}
			if(map.get("floor_num")!=null) {
				hql += "and  floor_num " + Util.decide(map.get("floor_num"));
			}
			if(map.get("school_id")!=null) {
				hql += "and  school_id" + Util.decide(map.get("school_id"));
			}

		}

		List<Unit> list = unitDao.find(hql,null,page.getPage(),page.getLimit());

		for (Unit item : list) {
			GQLBuilding gqlBuilding = null;
			if (env.getSelectionSet().contains("data/building")){
				Buildings buildings = item.getMaterialBuildingsByBuildingsId();
				gqlBuilding = BuildingMapper.MAPPER.toBuilding(buildings);
			}

			GQLSchool GgqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				GgqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			GQLUnit unit = UnitMapper.MAPPER.toUnit(item);
			unit.setBuilding(gqlBuilding);
			unit.setSchool(GgqlSchool);
			GQLUnit.add(unit);
		}
		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = unitDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLUnit).put("total", total);
	}
}
